String Methods in python, JavaScript and GO
String methods in Python, JavaScript and GO
Python:
# capitalize()
string = "hello, world!"
capitalized_string = string.capitalize()
print(capitalized_string) # "Hello, world!"
# lower()
string = "HELLO, WORLD!"
lowercase_string = string.lower()
print(lowercase_string) # "hello, world!"
# upper()
string = "hello, world!"
uppercase_string = string.upper()
print(uppercase_string) # "HELLO, WORLD!"
# strip()
string = " hello, world! "
stripped_string = string.strip()
print(stripped_string) # "hello, world!"
# replace()
string = "hello, world!"
replaced_string = string.replace("hello", "hi")
print(replaced_string) # "hi, world!"
# str.startswith() example
string = "Hello, World!"
if string.startswith("Hello"):
print("The string starts with 'Hello'")
# str.split() example
string = "Hello, World!"
words = string.split(",")
print(words) # Output: ['Hello', ' World!']
# str.format() example
name = "Alice"
age = 25
print("My name is {} and I'm {} years old".format(name, age)) # Output: "My name is Alice and I'm 25 years old"
# str.count() example
string = "Hello, World!"
count = string.count("l")
print(count) # Output: 3
# str.isalpha() example
string = "Hello"
if string.isalpha():
print("The string contains only letters")
- str.startswith(prefix[, start[, end]]):
Returns True if the string starts with the specified prefix, False
otherwise.
- str.endswith(suffix[, start[, end]]): Returns True
if the string ends with the specified suffix, False otherwise.
- str.split([sep[, maxsplit]]): Returns a list
of the words in the string, separated by the specified separator.
- str.join(iterable): Returns a string that is
the concatenation of the strings in the iterable, separated by the
original string.
- str.format(*args, **kwargs): Returns a
formatted string, where placeholders in the original string are replaced
by values passed as arguments or keyword arguments.
- str.count(sub[, start[, end]]): Returns the
number of non-overlapping occurrences of a substring in the string.
- str.index(sub[, start[, end]]): Returns the
lowest index in the string where a substring is found, raises a ValueError
if it is not found.
- str.find(sub[, start[, end]]): Returns the
lowest index in the string where a substring is found, returns -1
if it is not found.
- str.isalnum(): Returns True if all
characters in the string are alphanumeric (letters or numbers), False
otherwise.
- str.isalpha(): Returns True if all characters in the string are alphabetic (letters), False otherwise.
JavaScript:
// toUpperCase()
let string = "hello, world!";
let uppercaseString = string.toUpperCase();
console.log(uppercaseString); // "HELLO, WORLD!"
// toLowerCase()
string = "HELLO, WORLD!";
let lowercaseString = string.toLowerCase();
console.log(lowercaseString); // "hello, world!"
// trim()
string = " hello, world! ";
let trimmedString = string.trim();
console.log(trimmedString); // "hello, world!"
// replace()
string = "hello, world!";
let replacedString = string.replace("hello", "hi");
console.log(replacedString); // "hi, world!"
// str.startsWith() example
let string = "Hello, World!";
if (string.startsWith("Hello")) {
console.log("The string starts with 'Hello'");
}
// str.split() example
let string = "Hello, World!";
let words = string.split(",");
console.log(words); // Output: ['Hello', ' World!']
// str.replace() example
let string = "Hello, World!";
let newString = string.replace(",", ";");
console.log(newString); // Output: "Hello; World!"
// str.indexOf() example
let string = "Hello, World!";
let index = string.indexOf("l");
console.log(index); // Output: 2
// str.includes() example
let string = "Hello, World!";
if (string.includes("World")) {
console.log("The string contains 'World'");
}
- str.startsWith(searchString[, position]):
Returns true if the string starts with the specified search string,
false otherwise.
- str.endsWith(searchString[, length]): Returns true
if the string ends with the specified search string, false
otherwise.
- str.split([separator[, limit]]): Returns an
array of the words in the string, separated by the specified separator.
- str.concat(string2, string3, ..., stringX):
Returns a new string that is the concatenation of two or more strings.
- str.replace(regexp|substr, newSubStr|function):
Returns a new string with all matches of a regular expression or a
substring replaced by a new substring or the result of a function.
- str.indexOf(searchValue[, fromIndex]): Returns
the index of the first occurrence of a specified value in the string, or -1
if it is not found.
- str.lastIndexOf(searchValue[, fromIndex]):
Returns the index of the last occurrence of a specified value in the
string, or -1 if it is not found.
- str.charAt(index): Returns the character at
the specified index in the string.
- str.charCodeAt(index): Returns the Unicode
value of the character at the specified index in the string.
- str.includes(searchString[, position]):
Returns true if the string contains the specified search string, false
otherwise.
GO:
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
// strings.HasPrefix() example
string := "Hello, World!"
if strings.HasPrefix(string, "Hello") {
fmt.Println("The string starts with 'Hello'")
}
// strings.Split() example
string := "Hello, World!"
words := strings.Split(string, ",")
fmt.Println(words) // Output: ["Hello" " World!"]
// fmt.Sprintf() example
name := "Alice"
age := 25
message := fmt.Sprintf("My name is %s and I'm %d years old", name, age)
fmt.Println(message) // Output: "My name is Alice and I'm 25 years old"
// strings.Count() example
string := "Hello, World!"
count := strings.Count(string, "l")
fmt.Println(count) // Output: 3
// unicode.IsLetter() example
r := 'a'
if unicode.IsLetter(r) {
fmt.Println("The character is a letter")
// ToUpper()
string := "hello, world!"
uppercaseString := strings.ToUpper(string)
fmt.Println(uppercaseString) // "HELLO, WORLD!"
// ToLower()
string = "HELLO, WORLD!"
lowercaseString := strings.ToLower(string)
fmt.Println(lowercaseString) // "hello, world!"
// TrimSpace()
string = " hello, world! "
trimmedString := strings.TrimSpace(string)
fmt.Println(trimmedString) // "hello, world!"
// Replace()
string = "hello, world!"
replacedString := strings.Replace(string, "hello", "hi", -1)
fmt.Println(replacedString) // "hi, world!"
}
}
- strings.HasPrefix(s, prefix string): Returns true
if the string starts with the specified prefix, false otherwise.
- strings.HasSuffix(s, suffix string): Returns true
if the string ends with the specified suffix, false otherwise.
- strings.Split(s, sep string): Returns a slice
of the words in the string, separated by the specified separator.
- strings.Join(a []string, sep string): Returns
a string that is the concatenation of the strings in the slice, separated
by the specified separator.
- fmt.Sprintf(format string, a ...interface{}):
Returns a formatted string, where placeholders in the original string are
replaced by values passed as arguments.
- strings.Count(s, sep string): Returns the
number of non-overlapping occurrences of a substring in the string.
- strings.Index(s, sep string): Returns the
lowest index in the string where a substring is found, returns -1
if it is not found.
- strings.LastIndex(s, sep string): Returns the
highest index in the string where a substring is found, returns -1
if it is not found.
- strings.Contains(s, substr string): Returns true
if the string contains the specified substring, false otherwise.
Comments
Post a Comment